package org.donhuan.rms.ui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.swt.graphics.Image;

public class ImageCache {
	private final Map<String, Image> imageMap = new HashMap<String, Image>();
	private static ImageCache INSTANCE;

	private ImageCache() {
	}

	public static ImageCache getInstance() {
		if (INSTANCE == null)
			INSTANCE = new ImageCache();
		return INSTANCE;
	}

	public Image getImage(String imageName) {
		if (imageName == null)
			return null;
		Image image = imageMap.get(imageName);
		if (image == null)
			image = Activator.getImageDescriptor(imageName).createImage();
		imageMap.put(imageName, image);
		return image;
	}

	public void dispose() {
		Iterator<Image> it = imageMap.values().iterator();
		while (it.hasNext())
			((Image) it.next()).dispose();
		imageMap.clear();
	}
}
